package com.example.testscrollstub;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private int mLastFirstVisibleItem = 0;
	private boolean mIsScrollingUp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final TextView scrollingOrNotScrolling = (TextView)findViewById(R.id.stubbbystub);

		final Animation zoomDisappear = AnimationUtils.loadAnimation(this, R.anim.zoom_disappear);
		final Animation growFromBottom= AnimationUtils.loadAnimation(this, R.anim.grow_from_bottom);
		final ListView lw = (ListView) findViewById(R.id.listView1);
		lw.setOnScrollListener(new OnScrollListener() {



			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				if (view.getId() == lw.getId()) {
					final int currentFirstVisibleItem = lw.getFirstVisiblePosition();

					if (currentFirstVisibleItem > mLastFirstVisibleItem) {
						mIsScrollingUp = false;
					} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
						mIsScrollingUp = true;
					}
					mLastFirstVisibleItem = currentFirstVisibleItem;
					
					if(mIsScrollingUp)
					{
						if(scrollingOrNotScrolling.getVisibility() == View.GONE)
						{
							scrollingOrNotScrolling.startAnimation(growFromBottom);
						}
						scrollingOrNotScrolling.setVisibility(View.VISIBLE);
					}
					else
					{
						if(scrollingOrNotScrolling.getVisibility() == View.VISIBLE)
						{
							scrollingOrNotScrolling.startAnimation(zoomDisappear);
						}
						scrollingOrNotScrolling.setVisibility(View.GONE);
					}
				}
			}
		});
		lw.setAdapter(new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				TextView textView = null;
				if(convertView == null)
				 textView = new TextView(MainActivity.this);
				else
					textView = (TextView) convertView;
				
				textView.setText(""+position);
				return textView;
			}
			
			@Override
			public long getItemId(int arg0) {
				return arg0;
			}
			
			@Override
			public Object getItem(int arg0) {
				return arg0;
			}
			
			@Override
			public int getCount() {
				return 100;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
